<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="9">
          <!-- 商品搜索框 -->
          <div class="grid-content bg-purple">
            <el-input
              placeholder="请输入内容"
              v-model="queryInfo.query"
              clearable
              @clear="getGoodsList"
              @input="getGoodsList"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getGoodsList"
              >
              </el-button>
            </el-input>
          </div>
        </el-col>
        <!-- 添加商品按钮 -->
        <el-col :span="6">
          <div class="grid-content bg-purple">
            <el-button
              type="primary"
              @click="addGoods"
            >添加商品</el-button>
          </div>
        </el-col>
      </el-row>
      <el-table
        :data="goodsList"
        border
        stripe
      >
        <el-table-column
          :data="goodsList"
          type="index"
          label="#"
        >
        </el-table-column>
        <el-table-column
          label="商品名称"
          prop="goods_name"
          width="500px"
        >
        </el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="105px"
        >
        </el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="80px"
        >
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="add_time"
        >
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editGoods()"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 3, 5, 7, 9, 13]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 一个table -->
    <!-- 编辑商品的 Dialog -->
    <el-dialog
      title="编辑商品"
      :visible.sync="isEditGoodsDialogVisiable"
      width="50%"
      center
      @closed="editGoodsClosed"
    >
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      addGoodsInfo: {
        goods_name: '',
        goods_cat: '',
        goods_price: '',
        goods_number: '',
        goods_weight: ''
      },
      goodsList: [],
      isEditGoodsDialogVisiable: false,
      total: 0
    }
  },
  methods: {
    // 获得商品列表信息
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败!')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    addGoods () {
      this.$router.push('goods/add')
    },
    editGoods () {
      this.isEditGoodsDialogVisiable = true
    },
    // 添加商品对话框关闭时
    addGoodsClosed () { },
    // 编辑商品对话框关闭时
    editGoodsClosed () { },
    // 页数量发生变化时
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 当前页发生变化时
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    async deleteGoods (goodsId) {
      const confirm = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirm !== 'confirm') {
        return 0
      }
      const { data: res } = this.$http.delete(`goods/${goodsId}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败!')
      }
      this.$message.success('删除成功!')
      this.getGoodsList()
    }
  },
  created () {
    this.getGoodsList()
  }
}
</script>

<style lang="less" scoped>
.el-row {
  padding: 15px;
}
.el-pagination {
  margin-top: 15px;
  text-align: center;
}
</style>
